package com.skycong.hello77.activity.spinner;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import com.skycong.hello77.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ruanmingcong
 * @date 2018/1/14 19:13
 */
public class SpinnerActivity extends Activity implements AdapterView.OnItemSelectedListener{
    private TextView city;
    private Spinner spinner;
    private List<String> dataList;
    private ArrayAdapter<String> arr_adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner);
        dataList = new ArrayList<String>();
        dataList.add("北京");
        dataList.add("上海");
        dataList.add("广州");
        dataList.add("深圳");

        city = (TextView) findViewById(R.id.city);
        spinner = (Spinner) findViewById(R.id.spinner);
        spinner.setOnItemSelectedListener(this);
        arr_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,dataList);
        //arr_adapter设置下拉样式
        arr_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spinner.setAdapter(arr_adapter);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String cityName = arr_adapter.getItem(position);
        city.setText("你选着的城市是："+cityName);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
